package org.me.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.List;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class CourseSchedule implements Parcelable {

	private String courseScheduleId;
	private String courseId;
	private String courseName;
	private int dayOfWeek;
	private String room;
	private int startingPeriod;
	private List<Announcement> annList = new ArrayList<Announcement>();
	//private List<String> annList = new ArrayList<String>();
	public static final String Log_Tag = "CourseSchedule";

	public CourseSchedule() {
		annList = new ArrayList<Announcement>();
		
	}

	public CourseSchedule(SoapObject obj) {
		super();
		try {

			this.courseScheduleId = obj.getPropertySafelyAsString("courseScheduleId");
			this.courseId = obj.getPropertySafelyAsString("courseId");
			this.courseName = obj.getPropertySafelyAsString("courseName");
			this.dayOfWeek = Integer.parseInt( obj.getPropertySafelyAsString("dayOfWeek"));
			this.room = obj.getPropertySafelyAsString("room");
			this.startingPeriod = Integer
			.parseInt(obj.getPropertySafelyAsString("startingPeriod"));
			
			
			List<Announcement> annList = new ArrayList<Announcement>();
			//List<String> annsList = new ArrayList<String>();
			if (obj.hasProperty("annList")) {// /check whether annList exist in
												// courseSchedule list				
				for (int i = 0; i < obj.getPropertyCount(); i++) {
					PropertyInfo info = new PropertyInfo();
					obj.getPropertyInfo(i, info);	
					String tmpe = info.getName();
					if(tmpe.equalsIgnoreCase("annList")){
						SoapObject objAnn =  (SoapObject) info.getValue();
						
						Announcement announcement = new Announcement(objAnn,getCourseScheduleId());
						annList.add(announcement);
					}					
				}
			}

			this.annList = annList;
			

		} catch (Exception e) {

			Log.e(Log_Tag, "parse soap obj exception", e);

		}

	}

	

	public String getCourseScheduleId() {
		return courseScheduleId;
	}

	public void setCourseScheduleId(String courseScheduleId) {
		this.courseScheduleId = courseScheduleId;
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public int getDayOfWeek() {
		return dayOfWeek;
	}

	public void setDayOfWeek(int dayOfWeek) {
		this.dayOfWeek = dayOfWeek;
	}

	public String getRoom() {
		return room;
	}

	public void setRoom(String room) {
		this.room = room;
	}

	public int getStartingPeriod() {
		return startingPeriod;
	}

	public void setStartingPeriod(int startingPeriod) {
		this.startingPeriod = startingPeriod;
	}

	public List<Announcement> getAnnList() {
		return annList;
	}

	public void setAnnList(List<Announcement> annList) {
		this.annList = annList;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return 	courseName + "\n" + 
				courseId + " \n "  ;
	}
	
	
	

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int flags) {
		out.writeString(courseScheduleId);
		out.writeString(courseId);
		out.writeString(courseName);
		out.writeString(room);
		out.writeInt(dayOfWeek);
		out.writeInt(startingPeriod);
		out.writeList(annList);
		
	}
	public static final Parcelable.Creator<CourseSchedule> CREATOR = new Parcelable.Creator<CourseSchedule>() {
        public CourseSchedule createFromParcel(Parcel in) {
            return new CourseSchedule(in);
        }

        public CourseSchedule[] newArray(int size) {
            return new CourseSchedule[size];
        }
    };
    
    public CourseSchedule(Parcel in) {
    	readFromParcel(in);
	}

	private void readFromParcel(Parcel in) {
		
		courseScheduleId = in.readString();
		courseId = in.readString();
		courseName = in.readString();
		room = in.readString();
		dayOfWeek = in.readInt();
		startingPeriod = in.readInt();
		in.readList(annList, Announcement.class.getClassLoader());
		
		
	}

}
